#Rem
Header: AGFX Framework - GOO type cursor
#End

Strict
Import mojo
Import agfx


#Rem
summary: TCursor
#End
Class TCursor


	Private
	Field reposition:Bool = False
	Field segments:TPivot[]
	Field count:Int = 10
	Field body_offset:Float = 8
	Field start_scale:Float = 16
	Field end_scale:Float = 8
	Field border:Int = 3
	Field enable:Bool
	Field finalize:Bool=False

	
	Public
	#Rem
	summary:  Set color for body
	#End
	Field color_body:TColor
	#Rem
	summary: Set color fro outline
	#End
	Field color_border:TColor	
	
	#Rem
	summary: Create cursor
	ss:Int	- start element size in pixels
	es:Int	- end element size in pixels
	off:Float - offset between bodies
	count:Int - elements count
	speed:Float - speed for move to first point
	#End
	Method New(ss:Int,es:Int,off:Float,count:Int,speed:Float)
	
		Self.color_body = New TColor()
		Self.color_body.WHITE()
		Self.color_border = New TColor()
		Self.color_border.BLACK()
		
		Self.count = count
		Self.start_scale =ss
		Self.end_scale =es
		Self.segments = New TPivot[Self.count]	
		Self.body_offset = off
	
		For Local i:=0 to Self.count-1
			Self.segments[i] = New TPivot("SEG_"+i,-100,-100,8,8)
			Self.segments[i].speed = speed
		Next
		
	End Method

	#Rem
	summary: Update
	Add this call to your OnUpdate APP loop
	#End
	Method Update:Void()
	
		Local dir:TVector2
		Local delta_dir:TVector2
		Local pos:TVector2
		Local length:Float
		
		If MOUSE.Hit() Then 
			Self.ResetPosition()
			Self.finalize 	= False
			Self.enable		= True
		EndIf
		
		If MOUSE.Down()  Then
		
			Self.SetPosition(MOUSE.x,MOUSE.y)
			
			For Local i:Int = 1 To Self.count-1			
			
				length = Self.segments[i].position.DistanceTo(Self.segments[i-1].position)
				
				If length>Self.body_offset Then
					Self.segments[i].LookAt(Self.segments[i-1].position,True)
					dir = Self.segments[i].direction
					delta_dir = VecMult(dir,Self.body_offset)
					pos = VecSub(Self.segments[i-1].position,delta_dir)
					Self.segments[i].position.Set(pos)	
			
				End If
			Next
		End If
		
		Local d:Float = 0.0
		
		For Local i:Int = 1 To Self.count-1
			
			Self.segments[i].MoveTo(Self.segments[i-1].position)						
			
			d += Self.segments[i].position.DistanceTo(Self.segments[i-1].position)			
		Next
		
		If Self.finalize And d=0.0 Then 			
			If Self.segments[0].position.Compare(Self.segments[Self.count-1].position) Then Self.enable=False
		EndIf
		
		If MOUSE.Up() Then Self.finalize = True
		
	
		
	End Method
	
	#Rem
	summary: Render
	Add this call to your OnRender APP loop
	#End
	Method Render:Void()
		
		If Self.enable Then
	
			Local scale:Float = Abs(Self.start_scale - Self.end_scale)/Self.count
	
			Self.color_border.Apply()
			For Local i:Int = 0 To Self.count-1
				'Self.segments[i].Draw()
				DrawCircle(Self.segments[i].position.x,Self.segments[i].position.y,border+Self.start_scale-i*scale)
	
			Next
			
			Self.color_body.Apply()
			For Local i:Int = 0 To Self.count-1
				'Self.segments[i].Draw()
				DrawCircle(Self.segments[i].position.x,Self.segments[i].position.y,Self.start_scale-i*scale)
				
			Next
		EndIf
		
	End Method

	Private

	Method ResetPosition:Void()
		For Local i:=0 to Self.count-1
			Self.segments[i].position.Set(MOUSE.x,MOUSE.y)
		Next
		
		Self.enable = True
		
	End Method
	
	
	Method Hide:Void()
		For Local i:=0 to Self.count-1
			Self.segments[i].position.Set(-100,-100)
		Next
	End Method
	
	Method SetPosition:Void(x:Int,y:Int)
		'For Local i:=0 to Self.count-1
			Self.segments[0].position.Set(x,y)
		'Next
	End Method	
	
End Class
